package com.wry.wry_test.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESUtil {

    private static final String SECRET_KEY = "16af8cce8c944126"; // 密钥，16位

    public static String encrypt(String content) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encrypted = cipher.doFinal(content.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encryptedContent) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedContent));
        return new String(decrypted);
    }

    public static void main(String[] args) throws Exception {
        String pwd = "password";
        String encryptedPwd = AESUtil.encrypt(pwd);
        System.out.println("Encrypted Password: " + encryptedPwd);
        System.out.println("Decrypted Password: " + AESUtil.decrypt("mVC7fE0X4zN15cs5Euz+t+SpTWpLGLWTFe4xDr5xzp8="));
    }
}